package com.xjust.service.impl;

import com.xjust.Mapper.CheckRequestMapper;
import com.xjust.Mapper.DisposalRequestMapper;
import com.xjust.Mapper.InspectionRequestMapper;
import com.xjust.Mapper.MedicalTechnologyMapper;
import com.xjust.pojo.CheckRequest;
import com.xjust.service.MedicalTechnologyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class MedicalTechnologyServiceImpl implements MedicalTechnologyService {
    @Autowired
   private CheckRequestMapper checkRequestMapper;
    @Autowired
    private MedicalTechnologyMapper medicalTechnologyMapper;
    @Autowired
    private InspectionRequestMapper inspectionRequestMapper;
    @Autowired
    private DisposalRequestMapper disposalRequestMapper;
    //获取患者检查信息
    @Override
    public List<Map<String, Object>> getMedicalTechnology(Integer registId,String checkState) {
        Integer[] medicalTechnologyId = checkRequestMapper.getMedicalTechnologyId(registId,checkState);
        List<Map<String, Object>> list=new ArrayList<>();
        for (Integer integer : medicalTechnologyId) {
            list.add(medicalTechnologyMapper.getMedicalTechnology(integer));
        }
        return list;
    }
    //提交申请
    @Override
    public void saveMedicalTechnology(List<Map<String,Object>> checkRequest) {
        LocalDateTime creationTime = LocalDateTime.now();
        for (Map<String, Object> stringObjectMap : checkRequest) {

            stringObjectMap.put("checkState","已开立");
            stringObjectMap.put("creationTime",creationTime);
            medicalTechnologyMapper.InsertMedicalTechnology(stringObjectMap);
        }
    }
    //提取检查、检验、处置项目
    @Override
    public List<Map<String, Object>> selectMedicalTechnology(String tech_code, String tech_name, String tech_type) {
        List<Map<String, Object>> list =  medicalTechnologyMapper.selectMedicalTechnology(tech_code,tech_name,tech_type);
        return list;
    }
    //获取患者检验信息
    @Override
    public List<Map<String, Object>> getMedicalTechnologyByinspection(Integer registId, String inspectionState) {
        Integer[] medicalTechnologyId = inspectionRequestMapper.getMedicalTechnologyId(registId,inspectionState);
        List<Map<String, Object>> list=new ArrayList<>();
        for (Integer integer : medicalTechnologyId) {
            list.add(medicalTechnologyMapper.getMedicalTechnology(integer));
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getAllMedicalTechnologyByRegisterId(Integer registerId, String techType) {
        List<Map<String, Object>> list = null;
        if("检查".equals(techType)){
            list = checkRequestMapper.selectAllCheck(registerId);
        }else if("检验".equals(techType)){
           list = inspectionRequestMapper.selectAllinspection(registerId);
        }else{
            list = disposalRequestMapper.selectAllDisposalRequest(registerId);
        }
        String str;
        String code;
        String price;
        for (Map<String, Object> map : list) {
            Integer id = (Integer) map.get("medical_technology_id");
            str = medicalTechnologyMapper.selectAllMedicalTechnologyByRegisterIdOfName(id, techType);
            map.put("tech_name",str);
            map.put("tech_type",techType);
            code = medicalTechnologyMapper.selectAllMedicalTechnologyByRegisterIdOfCode(id, techType);
            map.put("tech_code",code);
            price = medicalTechnologyMapper.selectAllMedicalTechnologyByRegisterIdOfPrice(id, techType);
            map.put("tech_price",price);
        }

        return list;
    }
    //获取患者处置信息
    @Override
    public List<Map<String, Object>> getMedicalTechnologyByDisposal(Integer registId, String disposalState) {
        Integer[] medicalTechnologyId = disposalRequestMapper.getMedicalTechnologyId(registId,disposalState);
        List<Map<String, Object>> list=new ArrayList<>();
        for (Integer integer : medicalTechnologyId) {
            list.add(medicalTechnologyMapper.getMedicalTechnology(integer));
        }
        return list;
    }

    @Override
    public List<Map<String,Object>> selectMedicalTechnologyByRegisterId(Integer registerId) {
        Integer[] ids = checkRequestMapper.selectMedicalTechnologyByRegisterId(registerId);
        String[] states = checkRequestMapper.selectState(registerId); // 获取状态数组
        Integer[] cids = checkRequestMapper.selectCids(registerId); // 获取检查ID数组

        List<Map<String, Object>> list = medicalTechnologyMapper.selectMedicalTechnologyByRegisterId(ids);

// 假设 states.length 和 cids.length 是相等的，这里做一个假设处理
        int length = states.length; // 或者 cids.length，反正两者应该是相等的

        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i); // 获取当前的 Map 对象

            // 设置状态
            if (i < length) {
                map.put("check_state", states[i]);
            }

            // 设置检查ID
            if (i < cids.length) {
                map.put("check_id", cids[i]);
            }
        }

        return list;
    }


}
